/**
 * There are <a href="https://www.hdzhx.com">邯郸智讯</a> code generation
 */
package com.boxin.ims.modules.wechat.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.Length;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 微信秒杀者Entity
 * @author Jakemanse
 * @version 2013-09-13
 */
@Entity
@Table(name = "wp_seckill_gainer")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SeckillGainer extends BaseEntity {
	
	private static final long serialVersionUID = 1L;
	private Long id; 		// 编号
	private WechatMember member;	//微信会员
	private User user;				//所属商家
	private String number ;			//秒杀中奖序列号
	private Seckill seckill;		//所属秒杀
	private QRCode qrCode;			//
	private String name; 	// 名称	//u奖品名称
	private String userName;		//会员名称	
	private String ip;		//请求IP
	private String status ;		//状态 ：0：秒杀成功   1：已兑换奖品  2：作废
	private Date createTime;	// 创建日期
	private Date endTime;	// 创建日期
	private Date updateTime;	//核销、操作时间
	private boolean valid;		//是否可用   true 可用   false
	@Transient
	private Date startTime;	//
	
	private String delFlag;	// 删除标记（0：正常；1：删除）
	private String remarks; // 备注
	
	
	private String createDayString; 

	public SeckillGainer() {
		this.createTime = new Date();
		this.delFlag = DEL_FLAG_NORMAL;
		this.status =  STATUS_NORMAL;
	}

	public SeckillGainer(Long id){
		this();
		this.id = id;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_wechat_seckillGainer")
	//@SequenceGenerator(name = "seq_wechat_seckillGainer", sequenceName = "seq_wechat_seckillGainer")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	
	@Length(min=1, max=200)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Length(min=0, max=255)
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	@NotNull
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Length(min=1, max=1)
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	@ManyToOne
	@JoinColumn(name="member_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public WechatMember getMember() {
		return member;
	}

	public void setMember(WechatMember member) {
		this.member = member;
	}

	@ManyToOne
	@JoinColumn(name="seckill_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public Seckill getSeckill() {
		return seckill;
	}

	public void setSeckill(Seckill seckill) {
		this.seckill = seckill;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@ManyToOne
	@JoinColumn(name="qrcode_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public QRCode getQrCode() {
		return qrCode;
	}

	public void setQrCode(QRCode qrCode) {
		this.qrCode = qrCode;
	}
	
	
	@Transient
	public boolean isValid(){
		java.util.Calendar c1=java.util.Calendar.getInstance();
		java.util.Calendar c2=java.util.Calendar.getInstance();
		try {
			c1.setTime(new Date());
			c2.setTime(endTime);
		} catch (Exception e) {
			System.err.println("格式不正确");
			return true;
		}
		int result = c1.compareTo(c2);
		if (result > 0){	//c1>c2  过期
			return false;
		}
		
//		Date nowDate = new Date();
//		if(nowDate.getTime() > endTime.getTime()){
//			return false;
//		}
		return true;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@ManyToOne
	@JoinColumn(name="user_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Transient
	public String getCreateDayString() {
		
		return DateUtils.formatDate(this.createTime, "yyyyMMdd");
	}

	public void setCreateDayString(String createDayString) {
		this.createDayString = createDayString;
	}

	@Transient
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	
	

	
	
	
}


